package 代码记录.校招笔试真题.笔试真题_2022.好未来;

import java.util.*;

/**
 * 不装了,凭记忆过了
 */
public class Test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int hang = sc.nextInt();
        int lie = sc.nextInt();
        int[][] arr = new int[hang][lie];
        for(int i = 0;i < arr.length;i++){
            for(int j = 0;j < arr[0].length;j++){
                arr[i][j] = sc.nextInt();
            }
        }
        int res = 0;
        for(int i = 0;i < arr.length;i++){
            for(int j = 0;j < arr[0].length;j++){
                if(arr[i][j] == 1){
                    dfs(arr,i,j);
                    res++;
                }
            }
        }
        System.out.println(res);
    }
    private static void dfs(int[][] arr, int i, int j){
        if(i < 0 || i >= arr.length || j < 0 || j >= arr[0].length){
            return;
        }

        if(arr[i][j] != 1){
            return;
        }
        arr[i][j] = 2;
        dfs(arr,i - 1,j);
        dfs(arr,i + 1,j);
        dfs(arr,i,j - 1);
        dfs(arr,i,j + 1);
    }
}
